﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Windows.Forms;
using System.Windows.Media;
using System.Windows;

namespace MontagsmalerApp.Utils
{
	/// <summary>
	/// Stellt statisch Methoden und Variablen zur Verfügung, die in der gesamten Anwendung benötigt werden.
	/// </summary>
	public static class ConfigurationParameters
	{
    /// <summary>
    /// Gibt den in der Configdatei angegebenen Timerwert für die gesamte Spielrunde zurück.
    /// </summary>
    /// <returns>Timer in Sekunden.</returns>
    public static int GetTimerSpielrundeFromConfig()
    {
      string timerStr = ConfigurationManager.AppSettings.Get("timerSpielrunde");
      return int.Parse(timerStr);
    }

    /// <summary>
		/// Gibt den in der Configdatei angegebenen Timerwert für den Begriffszeitraum zurück.
		/// </summary>
		/// <returns>Timer in Sekunden.</returns>
		public static int GetTimerBegriffFromConfig()
		{
      string timerStr = ConfigurationManager.AppSettings.Get("timerBegriff");
			return int.Parse(timerStr);
		}

		/// <summary>
		/// Speicherort für die Teams (intern). 
		/// </summary>
		/// <returns></returns>
		public static string GetTeamSpeicherort()
		{
			string speicherort = ConfigurationManager.AppSettings.Get("teamSpeicherort");
			return speicherort;
		}

		/// <summary>
		/// Speicherort für die Begriffe (Xml).
		/// </summary>
		/// <returns></returns>
		public static string GetBegriffeSpeicherort()
		{
			string speicherort = ConfigurationManager.AppSettings.Get("begriffeSpeicherort");
			return speicherort;
		}

    /// <summary>
    /// Speicherort für die Begriffe (Xml).
    /// </summary>
    /// <returns></returns>
    public static bool GetWebCamNutzen()
    {
      bool retVal = false;
      string webCamNutzen = ConfigurationManager.AppSettings.Get("WebCamNutzen");

      bool.TryParse(webCamNutzen, out retVal);

      return retVal;
    }

    /// <summary>
    /// Inks the height of the canvas1 default drawing attributes.
    /// </summary>
    /// <returns></returns>
    public static double InkCanvas1DefaultDrawingAttributesHeight()
    {
      double i = 5;

      string height = ConfigurationManager.AppSettings.Get("InkCanvas1.DefaultDrawingAttributes.Height");
      double.TryParse(height, out i);
      
      return i;
    }

    /// <summary>
    /// Inks the width of the canvas1 default drawing attributes.
    /// </summary>
    /// <returns></returns>
    public static double InkCanvas1DefaultDrawingAttributesWidth()
    {
      double i = 5;

      string width = ConfigurationManager.AppSettings.Get("InkCanvas1.DefaultDrawingAttributes.Width");
      double.TryParse(width, out i);

      return i;
    }

    /// <summary>
    /// Inks the color of the canvas1 default drawing attributes.
    /// </summary>
    /// <returns></returns>
    public static Color InkCanvas1DefaultDrawingAttributesColor()
    {
      Color c;

      try
      {
        if (ConfigurationManager.AppSettings.Get("InkCanvas1.DefaultDrawingAttributes.Color") != null)
        {
          System.Drawing.Color cTemp = System.Drawing.ColorTranslator.FromHtml((ConfigurationManager.AppSettings.Get("InkCanvas1.DefaultDrawingAttributes.Color")));
          c = Color.FromArgb(cTemp.A, cTemp.R, cTemp.G, cTemp.B);
        }
        else
        {
          c = Colors.Red;
        }
      }
      catch (Exception ex)
      {
        c = Colors.Red;
      }

      return c;
    }

    /// <summary>
    /// Sets the value.
    /// </summary>
    /// <param name="key">The key.</param>
    /// <param name="value">The value.</param>
    public static void SetValue(string key, string value)
    {
      Configuration c = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
      if (c.AppSettings.Settings.AllKeys.Contains(key))
        c.AppSettings.Settings.Remove(key);

      c.AppSettings.Settings.Add(key, value);      
      c.Save();
    }
	}
}
